<?php
/**
 * @version 1.0
 * @created 08-Jun-2009 3:16:46 PM
 */
class ReadRole
{

	protected static $dataValidator;

	function __construct()
	{
	}

	/**
	 * 
	 * @param user
	 * @param mysqli
	 */
	public static function getAllRoles( $mysqliConnection = null )
	{
		if( Cache::getInstance()->get("roles") != false ){
			//error_log("getting data from cache", 0);
			return Cache::getInstance()->get("roles");
		}
		$results = array();
		// Instantiate the AmazonSDB class
		$sdb = new AmazonSDB();
		// Store the name of the domain
		$domain = ROLES_DOMAIN;
		// Create the domain
		$roles_domain = $sdb->domain_metadata( $domain );
		if ($roles_domain->isOK())
		{
			//create the query
			$sql = "select * from " . ROLES_DOMAIN;
			$response = $sdb->select( $sql );
			if ($response->isOK())
			{
				$response = json_decode( json_encode( $response ) );
			}else{
				return AwsUtils::parseError( $response );
			}
		}else{
			return AwsUtils::parseError( $roles_domain );
		}
		foreach( $response->{'body'}->{'SelectResult'}->{'Item'} as $roleObj ){
			$role = new Role();
			foreach( $roleObj->{'Attribute'} as $itemObj ){
				if( $itemObj->{'Name'} == 'keys' ){
					$role->{ $itemObj->{'Name'} } = explode(",", $itemObj->{'Value'} );
				}else{
					$role->{ $itemObj->{'Name'} } = $itemObj->{'Value'};
				}
			}
			$results[$role->name] = $role;
		}
		Cache::getInstance()->add("roles", $results, false, 86400);
		return $results;
	}

}
?>